﻿#pragma once
#include "PropertyEditView.h"

#include <QVBoxLayout>
#include <QLabel>
#include <QScrollArea>
#include <QSplitter>

#pragma execution_character_set("utf-8")

class PropertyEditViewUi {
    PropertyEditView *mWidget{nullptr};
private:
    QString TR(const QString &sourceText, const QString &disambiguation = "", int n = -1);
public:
    PropertyEditViewUi() = default;
    void SetupUI(PropertyEditView *widget);
    void RetranslateUI();

public:
    QLabel *labelObjectInfo{nullptr};
    QSplitter *headerSplitter{nullptr};
    QLabel *labelHeaderProperty{nullptr};
    QLabel *labelHeaderValue{nullptr};
    QWidget *columnSplitter{nullptr};
    QScrollArea *scrollAreaEditor{nullptr};
    QVBoxLayout *layoutEditors{nullptr};
};

inline QString PropertyEditViewUi::TR(const QString &sourceText, const QString &disambiguation, int n) {
    return mWidget->tr(sourceText.toStdString().c_str(), disambiguation.toStdString().c_str(), n);
}

inline void PropertyEditViewUi::SetupUI(PropertyEditView *widget) {
    mWidget = widget;

    labelObjectInfo = new QLabel;
    labelObjectInfo->setFixedHeight(20);
    labelObjectInfo->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
    //labelObjectInfo->setStyleSheet("QLabel { border: 1px solid #B8B8B8; }");

    labelHeaderProperty = new QLabel;
    //labelHeaderProperty->setStyleSheet("border: 1px solid #424343");
    labelHeaderProperty->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
    labelHeaderValue = new QLabel;
    //labelHeaderValue->setStyleSheet("border: 1px solid #424343");
    labelHeaderValue->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);

    headerSplitter = new QSplitter(Qt::Horizontal);
    headerSplitter->setFixedHeight(20);
    headerSplitter->setHandleWidth(2);
    headerSplitter->addWidget(labelHeaderProperty);
    headerSplitter->addWidget(labelHeaderValue);
    headerSplitter->setStyleSheet(R"(
        QSplitter {
            border: 1px solid #424343;
        }
        QSplitter::handle {
            background: #CBCBCB;
        }
    )");

    QObject::connect(headerSplitter, &QSplitter::splitterMoved, mWidget, &PropertyEditView::onHeaderSplitterMoved);

    layoutEditors = new QVBoxLayout;
    layoutEditors->setContentsMargins(0,0,0,0);
    layoutEditors->setSpacing(1);

    const auto editorContainer = new QWidget;
    editorContainer->setObjectName("EntryContainer");
    editorContainer->setLayout(layoutEditors);
    editorContainer->setStyleSheet(R"(
        QWidget#EntryContainer {
            background-color: #C8C8C8;
        }
    )");

    const auto layoutContents = new QVBoxLayout;
    layoutContents->setContentsMargins(0,0,0,0);
    layoutContents->setSpacing(0);
    layoutContents->addWidget(editorContainer, 0);
    layoutContents->addStretch(1);
    const auto contentsWidget  = new QWidget;
    contentsWidget->setLayout(layoutContents);

    scrollAreaEditor = new QScrollArea;
    scrollAreaEditor->setObjectName("EntryScrollArea");
    scrollAreaEditor->setFrameStyle(QFrame::NoFrame);
    scrollAreaEditor->setWidget(contentsWidget);
    scrollAreaEditor->setWidgetResizable(true);

    const auto layout = new QVBoxLayout;
    layout->setContentsMargins(0,0,0,0);
    layout->setSpacing(0);
    layout->addWidget(labelObjectInfo, 0, Qt::AlignLeft | Qt::AlignTop);
    layout->addWidget(headerSplitter);
    layout->addWidget(scrollAreaEditor, 1);
    mWidget->setLayout(layout);
    RetranslateUI();

    mWidget->resize(360, 640);
}

inline void PropertyEditViewUi::RetranslateUI() {
    mWidget->setWindowTitle(TR("属性查看器"));
    labelHeaderProperty->setText(TR("属性"));
    labelHeaderValue->setText(TR("值"));
}
